package com.tanhua.service;

import com.tanhua.domain.mongo.Video;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.util.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class VideoMQService {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Reference
    private VideoApi videoApi;

    public void sendSaveVideo(String videoId){
        sendMessage(videoId,1);
    }
    public void sendLikeVideo(String videoId){
        sendMessage(videoId,2);
    }
    public void sendUnlikeVideo(String videoId){
        sendMessage(videoId,3);
    }
    public void sendCommentVideo(String videoId){
        sendMessage(videoId,4);
    }

    public void sendMessage(String videoId,Integer type){
        Video video = videoApi.findVideoById(Long.parseLong(videoId));
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", UserHolder.getUser().getId());
        map.put("vid",video.getVid());
        map.put("type",type);
        rocketMQTemplate.convertAndSend("tanhua-video",map);
    }
}
